

package com.bei_en.teacher.management.controller.subject;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.education.condition.EducationCondition;
import com.bei_en.teacher.admin.model.education.vo.EducationVo;
import com.bei_en.teacher.admin.model.regions.condition.RegionsCondition;
import com.bei_en.teacher.admin.model.regions.vo.RegionsVo;
import com.bei_en.teacher.admin.model.subject.condition.SubjectCondition;
import com.bei_en.teacher.admin.model.subject.vo.SubjectVo;
import com.bei_en.teacher.admin.service.education.EducationService;
import com.bei_en.teacher.admin.service.regions.RegionsService;
import com.bei_en.teacher.admin.service.subject.SubjectService;
import com.bei_en.teacher.management.controller.BaseController;
import com.bei_en.teacher.management.web.MyJsonFailResult;
import com.bei_en.teacher.management.web.MyJsonResult;
import com.bei_en.teacher.management.web.MyJsonSuccessResult;

/**
 * 
 * @author Guocg
 * @date 2016-9-26 22:19:05
 */
@Controller
@RequestMapping("/admin")
public class SubjectController extends BaseController {
	   @Autowired
	    RegionsService regionsService;
    @Autowired
    SubjectService subjectService;
	   @Autowired
	    EducationService educationService;
    private void addSharedAttr(Model model) {
    	
    }

    @RequestMapping("/subject")
    public String subject(Model model,SubjectCondition vo, HttpServletResponse resp ) {

		List<SubjectVo> list =new ArrayList<SubjectVo>();
		try {
		    list = subjectService.findSubjectPage(vo);
		} catch (Exception e) {
		    e.printStackTrace();
		    writeJson(resp, new MyJsonFailResult(e.getMessage()));
		    return null;
		}
		model.addAttribute("Province", getProvince());
		model.addAttribute("City", getCity());
		model.addAttribute("Area", getArea());
		model.addAttribute("Education", getListEducation());
		model.addAttribute("list", list);
		addSharedAttr(model);
		model.addAttribute("vo", vo);
	
		return "/admin/subject/subjectList.html";
    }

    @RequestMapping("/subjectAdd")
    public String subjectAdd(Model model, SubjectVo vo, boolean show, HttpSession session,
	    HttpServletResponse resp) {

	if (show) {
	    addSharedAttr(model);
	    
		List<RegionsVo> list =new ArrayList<RegionsVo>();
		RegionsCondition regionvo=new RegionsCondition();
		regionvo.setPageSize(Integer.MAX_VALUE);
		regionvo.setQueryWhere("province_id=0&city_id=0&area_id=0");
		   list = regionsService.findRegionsPage(regionvo);
		   JSONObject object=null;
		   JSONArray array=new JSONArray();
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   object.accumulate("value", list.get(i).getItemId());
			   object.accumulate("lable", list.get(i).getItemName());
			   array.add(object);
		}
			model.addAttribute("regionsProvince", array);
	    return "/admin/subject/subjectAdd.html";
	}
	try {
	   /* SysUserVo obj = getLoginUser(session);
	    if (obj!=null)
		vo.setCreateId(obj.getId());
	    else
		vo.setCreateId(0);*/
		vo.setOrderBy(0);
	    vo.setCreateTime(new Date());
	subjectService.insert(vo);
	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}
	MyJsonSuccessResult re = new MyJsonSuccessResult();
	re.setCloseCurrent(true);
	re.setTabid("subjectList");
	writeJson(resp, re);
	return null;
    }

    @RequestMapping("/subjectEdit")
    public String subjectEdit(Model model,boolean show, SubjectVo vo, HttpSession session, HttpServletResponse resp) {
	
		if (show) {
		    SubjectVo vo1 = subjectService.findById(vo.getId());
		    addSharedAttr(model);
		    model.addAttribute("vo", vo1);
		    
		    List<RegionsVo> list =new ArrayList<RegionsVo>();
			RegionsCondition regionvo=new RegionsCondition();
			regionvo.setPageSize(Integer.MAX_VALUE);
			regionvo.setQueryWhere("province_id=0&city_id=0&area_id=0");
			   list = regionsService.findRegionsPage(regionvo);
			   JSONObject object=null;
			   JSONArray array=new JSONArray();
			   for (int i = 0; i < list.size(); i++) {
				   object=new JSONObject();
				   object.accumulate("value", list.get(i).getItemId());
				   object.accumulate("lable", list.get(i).getItemName());
				   array.add(object);
			}
			model.addAttribute("regionsProvince", array);
			
			RegionsCondition regionsCondition=new RegionsCondition();
			regionsCondition.setProvinceId(vo1.getProvinceId());
			regionsCondition.setAreaId(0);
			model.addAttribute("getRegionsCity", getRegionsCity(regionsCondition));
			
			regionsCondition=new RegionsCondition();
			regionsCondition.setCityId(vo1.getCityId());
			model.addAttribute("getRegionsArea", getRegionsArea(regionsCondition));
		    
			
			EducationCondition educationCondition=new EducationCondition();
			educationCondition.setCityId(vo1.getCityId());
			educationCondition.setProvinceId(vo1.getProvinceId());
			educationCondition.setAreaId(vo1.getAreaId());
			model.addAttribute("getEducationType", getEducationType(educationCondition));	
		    return "/admin/subject/subjectEdit.html";
		}
		try {
		   /* SysUserVo obj = getLoginUser(session);
		    if (obj!=null)
			vo.setUpdateId(obj.getId());
		    else
			vo.setUpdateId(0);*/
//		    vo.setUpdateTime(new Date());
		subjectService.updateSelective(vo);
		} catch (Exception e) {
		    e.printStackTrace();
		    writeJson(resp, new MyJsonFailResult(e.getMessage()));
		    return null;
		}
		MyJsonSuccessResult re = new MyJsonSuccessResult();
		re.setCloseCurrent(true);
		re.setTabid("subjectList");
		writeJson(resp, re);
		return null;
    }

    @RequestMapping("/subjectDel")
    @ResponseBody
    public MyJsonResult subjectDel(Model model, int[] delids, HttpSession session) {

		try {
		    for (int i : delids) {
			SubjectVo vo1 = new SubjectVo();
			vo1.setId(i);
//			vo1.setStatus(0);
			/*SysUserVo obj = getLoginUser(session);
			if (obj!=null)
			    vo1.setUpdateId(obj.getId());
			else
			    vo1.setUpdateId(0);*/
//			vo1.setUpdateTime(new Date());
			subjectService.delete(vo1.getId());
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		    return new MyJsonFailResult(e.getMessage());
		}
		return new MyJsonSuccessResult();
	}
    public JSONArray getEducationType(EducationCondition vo){
  	  List<EducationVo> list =new ArrayList<EducationVo>();
  		  list = educationService.findEducationPage(vo);
  	  JSONObject object=null;
  	   JSONArray array=new JSONArray();
  	   for (int i = 0; i < list.size(); i++) {
  		   object=new JSONObject();
  		   object.accumulate("value", list.get(i).getId());
  		   object.accumulate("label", list.get(i).getName());
  		   array.add(object);
  	}
  	return array;
         }
    
}
